package cndw.core;

import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;

import java.net.InetSocketAddress;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cndw.core.dispatch.DefaultRequest;
import cndw.core.dispatch.Dispatcher;
import cndw.core.dispatch.Request;
import cndw.core.dispatch.SessionManager;
import cndw.core.util.LogConst;

/**
 * @author zuojie.x
 */
public class GameServerHandler extends ChannelInboundMessageHandlerAdapter<Request> {
	private final Logger log = LoggerFactory.getLogger(LogConst.CORE);
	private Dispatcher dispatcher;
	private SessionManager sessionManager;
	
	public GameServerHandler(Dispatcher dispatcher, SessionManager sessionManager) {
		this.dispatcher = dispatcher;
		this.sessionManager = sessionManager;
	}

	@Override
	public void messageReceived(ChannelHandlerContext ctx, Request request)
			throws Exception {
		Channel channel = ctx.channel();
		((DefaultRequest) request).setSession(sessionManager.getSession(channel));
		dispatcher.dispatch(request);
	}
	
	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		super.exceptionCaught(ctx, cause);
	}
	
	@Override
	public void afterRemove(ChannelHandlerContext ctx) throws Exception {
		super.afterRemove(ctx);
		if (log.isDebugEnabled()) {
			InetSocketAddress address = ((InetSocketAddress) ctx.channel().remoteAddress());
			if (address != null)
				log.debug("{}:{} remove.", address.getAddress(), address.getPort());
		}
	}
	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		super.channelActive(ctx);
		if (log.isDebugEnabled()) {
			InetSocketAddress address = ((InetSocketAddress) ctx.channel().remoteAddress());
			if (address != null)
				log.debug("{}:{} channelActive.", address.getAddress(), address.getPort());
		}
	}
	
	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		super.channelInactive(ctx);
		if (log.isDebugEnabled()) {
			InetSocketAddress address = ((InetSocketAddress) ctx.channel().remoteAddress());
			if (address != null)
				log.debug("{}:{} channelInactive.", address.getAddress(), address.getPort());
		}
		sessionManager.removeSession(ctx.channel());
	}

	@Override
	public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
		super.channelRegistered(ctx);
		if (log.isDebugEnabled()) {
			InetSocketAddress address = ((InetSocketAddress) ctx.channel().remoteAddress());
			if (address != null)
				log.debug("{}:{} channelRegistered.", address.getAddress(), address.getPort());
		}
	}
	
	@Override
	public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
		super.channelUnregistered(ctx);
		if (log.isDebugEnabled()) {
			InetSocketAddress address = ((InetSocketAddress) ctx.channel().remoteAddress());
			if (address != null)
				log.debug("{}:{} channelUnregistered.", address.getAddress(), address.getPort());
		}
	}
}
